package StructuralPattern.Proxy.jdk_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <h3>23_Design_Patterns</h3>
 * <p></p>
 *
 * @author gdw
 * @since 2025-07-23 16:03:12
 */
public class ProxyFactory {
    private TrainStation trainStation=new TrainStation();

    public SellTickets getProxyObject(){
        //返回代理对象
        /*
          ClassLoader loader：  类加载器
          Class<?>[] interfaces：代理类实现接口的字节码对象
          InvocationHandler：代理对象的调用处理程序
         */
        SellTickets sellTickets= (SellTickets) Proxy.newProxyInstance(
                trainStation.getClass().getClassLoader(),
                trainStation.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("invoke 方法执行了");
                        Object obj = method.invoke(trainStation, args);
                        return obj;
                    }
                }
        );
        return sellTickets;
    }
}
